<!--
 * @Author: zengwj
 * @Date: 2022-09-21 17:00:39
 * @LastEditors: zengwj
 * @LastEditTime: 2022-09-21 17:25:09
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function promiseRace(promises) {
      if (!Array.isArray(promises)) {
        throw new Error('promises must be array')
      }
      let resolved = false
      return new Promise((resolve, rejct) => {
        promises.forEach(promise => {
          promise.then(data => {
            if (!resolved) {
              resolved = !resolved
              resolve(data)
            }

          })
        })
      })
    }

    function promsieAll(promises) {
      if (!Array.isArray(promises)) {
        throw new Error('promises must be array')
      }
      let len = 0
      const result = []
      return new Promise((resolve, rejct) => {
        try {
          promises.forEach(p => {
            p.then(data => {
              len++
              result.push(data)
              promises.length === len && resolve(result)
            }, error => {
              rejct(error)
            })
          })
        } catch (error) {
          rejct(error)
        }

      })
    }
    const p1 = new Promise((resolve, rejct) => {
      setTimeout(() => {
        resolve(1)
      }, 100);
    })
    const p2 = new Promise((resolve, rejct) => {
      setTimeout(() => {
        rejct(2)
      }, 200);
    })
    promsieAll([p1, p2]).then(data => {
      console.log(data)
    }, error => {
      console.log(error)
    })
    // promiseRace([p1, p2]).then(data => {
    //   console.log(data)
    // })
      //  Promise.all().then(()=>{

      //  })
  </script>
</body>

</html>